package com.kmair.caiwu.dbconn.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import java.sql.SQLException;

@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
    @Autowired
    private Environment env;

    //     配置DATASOURCES
    @Bean
    public DruidDataSource dataSourceMySQL() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(env.getProperty("mysql.url"));
        druidDataSource.setUsername(env.getProperty("mysql.username"));
        druidDataSource.setPassword(env.getProperty("mysql.password"));
        druidDataSource.setMaxActive(Integer.parseInt(env.getProperty("druid.max_size")));
        try {
            druidDataSource.setFilters("config");
        } catch (SQLException e) {
            e.printStackTrace();
        }
//         配置数据库加密
        druidDataSource.setConnectionProperties("config.decrypt=true;config.decrypt.key=" + env.getProperty("mysql.publickey"));
        return druidDataSource;
    }
}
